// components/profile-fill/index.js
const app = getApp();
const cwx = require('cloudfile');
Component({
  /**
   * 组件的一些选项
   */
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    nickName: '',
    avatarUrl: 'https://s1.ax1x.com/2023/04/13/ppx0hwt.png'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 点击确认
    confirm() {
      var that = this;
      if (this.data.nickName === '') {
        wx.showToast({
          title: '昵称不可为空',
          icon: 'error'
        })
      } else {
        wx.showLoading({
          title: '上传中',
        })
        wx.cloud.callFunction({
          name: 'coreapi',
          data: {
            action: 'putUserProfile',
            data: {
              avatarUrl: that.data.avatarUrl,
              nickName: that.data.nickName
            }
          },
          complete: res => {
            console.log(res)
            let userInfo = res.result.data;
            wx.setStorageSync('userInfo', userInfo)
            app.globalData.userInfo = userInfo
            wx.hideLoading()
            this.hideModal();
            this.triggerEvent('succ', null)
          }
        })
      }

    },
    onChooseAvatar(e) {
      const {
        avatarUrl
      } = e.detail
      wx.showLoading({
        title: '头像上传中',
      })
      cwx.CloudUploadImage(avatarUrl).then(res => {
        let fileID = res.fileID
        this.setData({
          avatarUrl: fileID
        })
        wx.hideLoading()
      })

    },
    showModal(e) {
      this.setData({
        show: true
      })
    },
    hideModal(e) {
      this.setData({
        show: false
      })
    },
  }
})